package com.topsec.manager.util;

import org.apache.http.util.TextUtils;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * Created by lx1 on 2016/4/20.
 */
public class DateUtil {
	public final static String PATTERN_yyyy_MM_dd = "yyyy-MM-dd hh:mm";
	public final static String PATTERN_yyyy_MM_dd_HH_mm_ss = "yyyy-MM-dd HH:mm:ss";
	public final static String PATTERN_yyyy_MM_dd_hh_mm_ss = "yyyy-MM-dd hh:mm:ss";

	public final static String PATTERN_yyyyMMdd = "yyyy/MM/dd";
	public final static String PATTERN_yyyyMMdd_dd_HH_mm_ss = "yyyy/MM/dd HH:mm:ss";
	public final static String PATTERN_yyyyMMdd_dd_hh_mm_ss = "yyyy/MM/dd hh:mm:ss";

	public static Date convertString2Date(String time) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(PATTERN_yyyy_MM_dd);
		Date parse = null;
		try {
			parse = simpleDateFormat.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parse;
	}

	public static String DateToString(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String dateStr = null;
		try {
			dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dateStr;
	}

	// 今天23点59分59秒的毫秒数
	public static long gettwelveOnTime() {
		// 当前时间毫秒数
		long current = System.currentTimeMillis();
		// 今天零点零分零秒的毫秒数
		long zero = current / (1000 * 3600 * 24) * (1000 * 3600 * 24) - TimeZone.getDefault().getRawOffset();
		// 今天23点59分59秒的毫秒数
		long twelve = zero + 24 * 60 * 60 * 1000 - 1;
		return twelve;
	}

	// 昨天这一时间点
	public static long getYesterOnTime() {
		// 昨天的这一时间的毫秒数
		long yesterday = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
		return yesterday;
	}

	// 当前时间的前（后）几小时
	public static String getBeforeHourTime(int ihour) {
		String returnstr = "";
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - ihour);
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		returnstr = df.format(calendar.getTime());
		return returnstr;
	}

	// 获得当天0点时间
	public static Date getTimesmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	// 获得当天24点时间
	public static Date getTimesnight() {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}

	// 获得本周一0点时间
	public static Date getTimesWeekmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}

	// 获得本周日24点时间
	public static Date getTimesWeeknight() {
		Calendar cal = Calendar.getInstance();
		cal.setTime(getTimesWeekmorning());
		cal.add(Calendar.DAY_OF_WEEK, 7);
		return cal.getTime();
	}

	// 获得本月第一天0点时间
	public static Date getTimesMonthmorning() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));

		return cal.getTime();
	}

	// 获得本月最后一天24点时间
	public static Date getTimesMonthnight() {
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
		cal.set(Calendar.HOUR_OF_DAY, 24);
		return cal.getTime();
	}

	/**
	 * 将java.util.Date对象转化为java.sql.Timestamp对象
	 *
	 * @param date
	 *            要转化的java.util.Date对象
	 * @return 转化后的java.sql.Timestamp对象
	 */
	public static java.sql.Timestamp dateToTime(Date date) {
		String strDate = dateToStr(date, "yyyy-MM-dd HH:mm:ss");
		return strToSqlDate(strDate, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 将java.util.Date对象转化为String字符串
	 *
	 * @param date
	 *            要格式的java.util.Date对象
	 * @param strFormat
	 *            输出的String字符串格式的限定（如："yyyy-MM-dd HH:mm:ss"）
	 * @return 表示日期的字符串
	 */
	public static String dateToStr(Date date, String strFormat) {
		SimpleDateFormat sf = new SimpleDateFormat(strFormat);
		String str = sf.format(date);
		return str;
	}

	/**
	 * 将String字符串转化为java.util.Date对象
	 *
	 * @param date
	 *            要格式的String
	 * @param strFormat
	 *            输出的java.util.Date格式的限定（如："yyyy-MM-dd HH:mm:ss"）
	 * @return 表示日期
	 */
	public static Date strToDate(String date, String strFormat) {
		SimpleDateFormat sf = new SimpleDateFormat(strFormat);
		Date parse = null;
		try {
			parse = sf.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return parse;
	}

	/**
	 * 将String字符串转换为java.sql.Timestamp格式日期,用于数据库保存
	 *
	 * @param strDate
	 *            表示日期的字符串
	 * @param dateFormat
	 *            传入字符串的日期表示格式（如："yyyy-MM-dd HH:mm:ss"）
	 * @return java.sql.Timestamp类型日期对象（如果转换失败则返回null）
	 */
	public static java.sql.Timestamp strToSqlDate(String strDate, String dateFormat) {
		SimpleDateFormat sf = new SimpleDateFormat(dateFormat);
		Date date = null;
		try {
			date = sf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		java.sql.Timestamp dateSQL = new java.sql.Timestamp(date.getTime());
		return dateSQL;
	}

	/**
	 * Java将Unix时间戳转换成指定格式日期字符串
	 *
	 * @param timestampString
	 *            时间戳 如："1473048265";
	 * @param formats
	 *            要格式化的格式 默认："yyyy-MM-dd HH:mm:ss";
	 * @return 返回结果 如："2016-09-05 16:06:42";
	 */
	public static String TimeStamp2Date(String timestampString, String formats) {
		if (TextUtils.isEmpty(formats))
			formats = "yyyy-MM-dd HH:mm:ss";
		Long timestamp = Long.parseLong(timestampString) * 1000;
		String date = new SimpleDateFormat(formats, Locale.CHINA).format(new Date(timestamp));
		return date;
	}

	// public static void main(String[] args) {
	// List<String> str = new ArrayList<>();
	// str.add("12");
	// List<String> strs = new ArrayList<>();
	//// strs.add("23423");
	//
	// str.addAll(strs);
	//
	// for (String s : str) {
	// System.out.println(s);
	// }
	// Date date = convertString2Date("2016-04-19 12:00");
	// System.out.println(date.toString());
	// }

	/**
	 * 计算两个Date类型日期之间相差的天数
	 *
	 * @param smdate
	 *            较小的时间
	 * @param bdate
	 *            较大的时间
	 * @return 相差天数
	 * @throws ParseException
	 */
	public static int daysBetween(Date smdate, Date bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		smdate = sdf.parse(sdf.format(smdate));
		bdate = sdf.parse(sdf.format(bdate));
		Calendar cal = Calendar.getInstance();
		cal.setTime(smdate);
		long time1 = cal.getTimeInMillis();
		cal.setTime(bdate);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个字符串类型日期之间相差的天数
	 */
	public static int daysBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个字符串类型日期之间相差的秒数
	 */
	public static String secBetween(String smdate, String bdate) throws ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sdf.parse(smdate));
		long time1 = cal.getTimeInMillis();
		cal.setTime(sdf.parse(bdate));
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1);
		System.out.println(" ===>>> endTime：" + time2 + " - starTime：" + time1 + " = " + between_days);

		float v = between_days;
		System.out.println(" ===>>> 当前 float 类型时间差：" + v);

		// 四舍五入
		BigDecimal b = new BigDecimal(v / 1000);
		double f1t = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
		System.out.println(" ===>>> 保留 3 为小数：" + f1t);
		String s = String.valueOf(f1t);
		System.out.println(" ===>>> 计算时间，四舍五入：" + s);
		if ("0.0".equals(s)) {
			s = "0.1";
		}
		return s;
	}
}
